package cn.wangzg.mymusic;

import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private List<Music> musicList;
    private int cur_pos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //mediaPlayer=MediaPlayer.create(this,R.raw.xf);
        mediaPlayer=new MediaPlayer();
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                next();
            }
        });
        musicList=getMusic();
        ListView listView=findViewById(R.id.list);
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return musicList.size();
            }

            @Override
            public Object getItem(int position) {
                return musicList.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView= LayoutInflater.from(MainActivity2.this)
                        .inflate(R.layout.music_item,parent,false);
                TextView title=convertView.findViewById(R.id.textView);
                TextView artist=convertView.findViewById(R.id.textView2);
                TextView duration=convertView.findViewById(R.id.textView3);
                title.setText(musicList.get(position).getTitle());
                artist.setText(musicList.get(position).getArtist());
                duration.setText(formatTime(musicList.get(position).getDuration()));
                return convertView;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(MainActivity2.this, musicList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
                play(position);
            }
        });
    }
    private String formatTime(long ms){
        SimpleDateFormat sdf=new SimpleDateFormat("mm:ss");
        return sdf.format(ms);
    }
    private void play(int position) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(musicList.get(position).getData());
            mediaPlayer.prepare();
            mediaPlayer.start();
            cur_pos= position;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void next(){
        ++cur_pos;
        if(cur_pos==musicList.size()) cur_pos=0;
        play(cur_pos);
    }
    private List<Music> getMusic(){
        List<Music> musics=new ArrayList<>();
        String[] projection={
                MediaStore.Audio.Media.TITLE,
          MediaStore.Audio.Media.ARTIST,
          MediaStore.Audio.Media.DATA,
          MediaStore.Audio.Media.DURATION
        };
        Cursor cursor = getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection, null, null, null);
        while (cursor.moveToNext()){
            //String title=cursor.getString(cursor.getColumnIndex("title"));
            String title=cursor.getString(0);
            String artist=cursor.getString(1);
            String data=cursor.getString(2);
            long duration=cursor.getLong(3);
            System.out.println(title+","+artist);
            musics.add(new Music(title,artist,data,duration));
        }
        cursor.close();
        return musics;
    }

    public void play(View view) {
        //mediaPlayer.start();

    }

    public void pause(View view) {
        mediaPlayer.pause();
    }

    public void stop(View view) {
        mediaPlayer.stop();
    }
}